package com.baizhi.controller;


import com.aliyun.oss.OSS;
import com.baizhi.config.MyConfig;
import com.baizhi.entity.Brand;
import com.baizhi.service.IBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author haiyu
 * @since 2021-09-16
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private OSS ossClient;

    @Autowired
    private IBrandService bs;
    @GetMapping("/selectAll")
    public List<Brand> selectAll(){

        List<Brand> list = bs.list();
        return  list;
    }


    //文件上传的方法,上传成功之后需要把文件上传到哪交给前端
    @PostMapping("/uploadPic")
    public String uploadPic(MultipartFile brandPic) throws IOException {
    //把图片保存起来。一般情况下我们会专门创建一个文件夹保存上传的图片
        String oldName = brandPic.getOriginalFilename();//获取到上传的文件的原始文件名
        String key = UUID.randomUUID().toString() + oldName;
//        File dz = new File("f:/myImg/"+s); 该方法存到自己电脑
//        brandPic.transferTo(dz);//把图片复制到某个位置

        InputStream in = brandPic.getInputStream();
        String bulkName ="bz-2103";
        ossClient.putObject(bulkName, key, in);
       // return key;//传到数据库
        return "https://"+bulkName+"."+ MyConfig.ENDPOINT+"/"+key;
    }


    //后台保存brand发方法
    @PostMapping("/saveBrand")
    public void saveBrand(@RequestBody Brand brand){
        bs.save(brand);
    }

}

